<?php
require './model/Db.php';
$Db = Db::getInstance();
$user_data = $Db->islogin();
$isExistc = $Db->find([
	table => ['content'],
	where => "id = {$_GET['c']} and delete_time is NULL"
])[0];
$w_arr = ['update','reply'];
$tt_arr = [update => '修改帖子',reply => 'Re'];
if($isExistc && in_array($_GET['w'], $w_arr)){
	if(!$user_data){
		$msg = '未登录,请先登录';
		$img_type = 'err';
		$reurl = 'login.php?err=403';
		include './view/skip.html';
		exit();
	}elseif($_GET['w'] == 'reply'){ 
	//回复
		if(is_numeric($_GET['r'])){
			$rid = $Db->find([
				table => ['content'],
				where => "id = {$_GET['r']} and delete_time is NULL"
			])[0];
			
			if(!$rid){
				header('Location:err.php?id=1');
			}else{
				if($rid['cid'] == 0){
					$isMain = true;
				}
				if(isset($_POST['submit'])){
					$_POST = $Db->escape($_POST);
					$paths = '';
					if($_POST['files']){
						foreach ($_POST['files'] as $value) {
							$paths .= '"'.$value.'",';
						}
						$paths = trim($paths,',');
					}
					$title = $isMain ? 'Re:'.$rid['title']:$rid['title'];
					$reply_id = $isMain ? 0:$rid['id'];
					$cid = $isMain ? $rid['id']:$rid['cid'];
				    
					$flag = $Db->save([
						table => 'content',
						map => [
							title => $title,
							body => $_POST['body'],
							mid => $rid['mid'],
							reply_id => $reply_id,
							file => '{"path":['.$paths.']}',
							uid => $user_data['id'],
							cid => $cid,
						]
					]);
					if($flag){
					 //加分
						$maxlv = 20+$user_data['job']*5;
						if($user_data['today_lv'] < $maxlv){
							if($user_data['today_lv']+2 > $maxlv){
								$diff = $user_data['today_lv']-$maxlv+2;
								$lv = "#lv+{$diff}#";
								$today_lv = $maxlv;
							}else{
								$lv = '#lv+2#';
								$today_lv = '#today_lv+2#';
							}
							$Db->update([
								table => 'bbs_user',
								map => [
									lv => $lv,
									today_lv => $today_lv
								],
								where => "id = {$user_data['id']}"
							]);
						}

						$ids = $isMain ? "{$rid['id']}":"{$rid['id']},{$rid['cid']}";
						$Db->update([
						table => 'content',
						map => [
							reply_num => '#reply_num+1#'
						],
						where => "id in ({$ids})"
					]);
					}
					$msg = $flag ? '回帖成功' : '网络不稳定,请稍后再试';
					$img_type = $flag ? 'success' : 'err';
					$count_ = $Db->count([
                		table => ['content'],
                		where => "id = {$_GET['c']} or cid = {$_GET['c']} and delete_time is NULL"
                	]);

                	$p = ceil($count_/PAGE_SIZE);
                	$rand = rand(1000,9999);
					$reurl = $flag ? "details.php?cid={$_GET['c']}&page={$p}&rand={$rand}#end" : "details.php?cid={$_GET['c']}";
					include './view/skip.html';
					exit();
				}
			
			}
		}else{
			header('Location:err.php?id=1');
		}
	}else{
	//修改
		if($isExistc['cid'] == 0){
			$isMain = true;
		}
		$file_arr = json_decode($isExistc['file'])->path;
		if(isset($_POST['submit'])){
			$_POST = $Db->escape($_POST);
			$paths = '';
			if($_POST['files']){
				foreach ($_POST['files'] as $value) {
					$paths .= '"'.$value.'",';
				}
				$paths = trim($paths,',');
			}
			$title = $isMain ? $_POST['title']:$isExistc['title'];
			$flag = $Db->update([
				table => 'content',
				map => [
					title => $title,
					body => $_POST['body'],
					file => '{"path":['.$paths.']}',
					update_time => '#now()#'
				],
				where => "id = {$isExistc['id']}"
			]);
			$msg = $flag ? '修改帖子成功' : '网络不稳定,请稍后再试';
			$img_type = $flag ? 'success' : 'err';
			$reurl = "usercenter.php";
			include './view/skip.html';
			exit();
		}
	}
	//获取本地表情
	$bq_arr = [];
	if(is_dir(EXP_FILE_PATH)){
		$dir_handle = opendir(EXP_FILE_PATH);
		while($file = readdir($dir_handle)){
			if(is_file(EXP_FILE_PATH.$file)){
				$bq_arr[$file] = EXP_FILE_PATH.$file;
			}
		}
	}
}else{
	header('Location:err.php?id=1');
}

$Db->close();

$web_title = $tt_arr[$_GET['w']];
$css = [
	'public/css/my-bootstrap.min.css',
	'public/css/publish.all.min.css'
];
include './view/header.html';
include './view/publish.all.html';
include './view/footer.html';